package com.bit.cfdi.web.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.bit.cfdi.web.dao.CampoExtraComprobanteDAO;
import com.bit.cfdi.web.domain.CampoExtraComprobante;
import com.bit.cfdi.web.domain.Giro;
import com.bit.cfdi.web.domain.Negocio;

@Repository
public class CampoExtraComprobanteDAOImpl extends HibernateDaoSupport implements CampoExtraComprobanteDAO {

	@Autowired
	public void init(final SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}
	
	@Transactional
	@Override	public List<CampoExtraComprobante> getAll() throws DataAccessException {
		return null;
	}

	@Transactional
	@Override	public List<CampoExtraComprobante> getById(Long id) throws DataAccessException {
		return null;
	}

	@Transactional
	@Override public void save(CampoExtraComprobante campoExtraComprobante) throws DataAccessException{
		getHibernateTemplate().saveOrUpdate(campoExtraComprobante);
	}
		
	@SuppressWarnings("unchecked")
	@Transactional
	@Override public List<CampoExtraComprobante> getDefault() throws DataAccessException {
		List<CampoExtraComprobante> camposExtraComprobante = getHibernateTemplate().find(
				" from CampoExtraComprobante as campoExtraComprobante " +
				" where campoExtraComprobante.negocio = null");
		return camposExtraComprobante;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	public List<CampoExtraComprobante> getByNegocio(Negocio neg) throws DataAccessException {
		List<CampoExtraComprobante> camposExtraComprobante = getHibernateTemplate().find(
				" from CampoExtraComprobante as campoExtraComprobante " +
				" where campoExtraComprobante.negocio = ?",neg);
		return camposExtraComprobante;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	public List<CampoExtraComprobante> getByDscr(String dscr,Negocio neg) throws DataAccessException {
		if(dscr==null) {
			dscr="";
		}
		List<CampoExtraComprobante> camposExtraComprobante = getHibernateTemplate().find(
				" from CampoExtraComprobante as campoExtraComprobante " +
				" where (campoExtraComprobante.negocio = null or campoExtraComprobante.negocio = ?) "
			   +" AND LOWER(campoExtraComprobante.nombreCampo) = ?",neg,dscr.toLowerCase());
		return camposExtraComprobante;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	public List<CampoExtraComprobante> getByStatusAndNegocio (
			Integer status, Negocio negocio) throws DataAccessException{
		List<CampoExtraComprobante> camposExtraComprobante = getHibernateTemplate().find(
				" from CampoExtraComprobante as campoExtraComprobante " +
				" where campoExtraComprobante.status = ? and (campoExtraComprobante.negocio = null or " +
				"campoExtraComprobante.negocio = ?)",status, negocio);
		return camposExtraComprobante;
	}
	
	@SuppressWarnings("unchecked")
	@Override	
	@Transactional public List<CampoExtraComprobante> 
	getByStatusDefaultAndNegocio(Integer status, Negocio negocio) throws DataAccessException {
		List<CampoExtraComprobante> camposExtraComprobante = getHibernateTemplate().find(
				" from CampoExtraComprobante as campoExtraComprobante " +
				" where (campoExtraComprobante.status = ? or campoExtraComprobante.status = null) " +
				" and (campoExtraComprobante.negocio = null or " +
				"campoExtraComprobante.negocio = ?)",status, negocio);
		return camposExtraComprobante;
	}
	
	@SuppressWarnings("unchecked")
	@Override 
	@Transactional public List<CampoExtraComprobante> getDefaultInit() throws DataAccessException {
		List<CampoExtraComprobante> camposExtraComprobante = getHibernateTemplate().find(
				" from CampoExtraComprobante as campoExtraComprobante " +
				" where campoExtraComprobante.negocio = null" +
				" and campoExtraComprobante.status = null");
		return camposExtraComprobante;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional public List<CampoExtraComprobante> getByGiro(Giro giro) throws DataAccessException {
		List<CampoExtraComprobante> campoExtraComprobante = null;
		campoExtraComprobante = getHibernateTemplate().find(
				"SELECT campoExtraComprobante " +
				"FROM CampoExtraComprobante as campoExtraComprobante," +				
				"GiroCampoExtraComprobante giroCampoExtraComprobante " +
				"WHERE giroCampoExtraComprobante.giro.idGiro = ? " +
				"AND campoExtraComprobante.idCampoExtraComprobante = " +
				"giroCampoExtraComprobante.campoExtraComprobante.idCampoExtraComprobante", giro.getIdGiro());
		return campoExtraComprobante;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional public CampoExtraComprobante getByIdentificador(
			String identificadorCampoExtra) throws DataAccessException {
		List<CampoExtraComprobante> camposExtraComprobante = getHibernateTemplate().find(
				" from CampoExtraComprobante as campoExtraComprobante " +
				" where campoExtraComprobante.identificador = ?", identificadorCampoExtra);
		return camposExtraComprobante.size()>0?camposExtraComprobante.get(0):null;
	}
	
	
}
